#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

"""
@File    :allocating.py
@Author  :keyin
@Time    :2021-02-19 10:50
"""
import os
import sys
from os.path import join

from PyQt5.QtWidgets import QApplication, QMessageBox
from PyQt5.QtCore import pyqtSignal
from PyQt5 import QtCore

from loaders.CardLoader import CardLoader, DetailLoader, CardCuidLoader
from main_controller import MainController

BasePath = os.path.abspath('.')
BasePath = join(BasePath, 'utils')

class AllocatingCenter(MainController):
    """
    分拨中心
    """

    # 实例化两个信号，一个加载页面，一个传递错误
    load_signal = pyqtSignal(str)
    err_signal = pyqtSignal(str)

    def __init__(self):
        super().__init__()
        # 信号s
        self.signals = {
            'load_signal': self.load_signal,
            'err_signal': self.err_signal,
        }
        # 信号连接动作
        self.load_signal.connect(self.load_one)
        self.err_signal.connect(self.err_raise)
        # 身份录入页面，参数是页面和信号
        self.card_load = CardLoader(self.card, self.signals)
        self.card_detail = DetailLoader(self.card, self.signals)
        self.card_cuid = CardCuidLoader(self.card, self.signals)

        # 需要自动加载的页面
        self.loadNeeding = [self.card_load]
        self.load_all()
        # 一些操作集
        self.action_all()

    def load_one(self, load_name):
        """
        加载一个页面
        :param load_name:
        :return:
        """
        self.__dict__[load_name].load()

    def load_all(self):
        for loader in self.loadNeeding:
            loader.load()

    def err_raise(self, err):
        """抛出某个错误提示"""
        return QMessageBox.critical(self, '错误', '{}'.format(err), QMessageBox.Yes)

    def action_all(self):
        self.action_card()
        self.action_app_out()

    def action_card(self):
        self.card.card_tableWidget.itemSelectionChanged.connect(self.card_detail.load)
        self.card.btn_base_New.clicked.connect(self.card_cuid.load)
        self.card.btn_base_Edit.clicked.connect(self.card_cuid.load)
        self.card.btn_base_Save.clicked.connect(self.card_cuid.load)
        self.card.btn_base_Del.clicked.connect(self.card_cuid.load)
        self.card.btn_base_Cancel.clicked.connect(self.card_cuid.load)
        self.card.lbl_Front.clicked.connect(self.card_cuid.load)
        self.card.lbl_Back.clicked.connect(self.card_cuid.load)

    def action_app_out(self):
        pass

if __name__ == '__main__':
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QApplication(sys.argv)
    print(BasePath)
    win = AllocatingCenter()
    win.show()
    sys.exit(app.exec_())